/**
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package sel.me.sharding.learning.algorithm;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;

import com.google.common.collect.Range;

import io.shardingsphere.api.algorithm.sharding.ShardingValue;
import io.shardingsphere.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm;

//public final class MultipleKeysModuloTableShardingAlgorithm implements ComplexKeysShardingAlgorithm {
public final class MultipleKeysModuloTableShardingAlgorithm {

//    private Collection<String> doEqualSharding(final Collection<String> availableTargetNames,
//                                               final ShardingValue shardingValue) {
//        for (String each : availableTargetNames) {
//            if (each.endsWith("_" + shardingValue.getValue() % availableTargetNames.size())) {
//                return Arrays.asList(each);
//            }
//        }
//        throw new UnsupportedOperationException();
//    }
//
//    private Collection<String> doInSharding(final Collection<String> availableTargetNames,
//                                            final ShardingValue shardingValue) {
//        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
//        Collection<Long> values = shardingValue.getValues();
//        for (Long value : values) {
//            for (String tableNames : availableTargetNames) {
//                if (tableNames.endsWith("_" + (value % availableTargetNames.size()) + "")) {
//                    result.add(tableNames);
//                }
//            }
//        }
//        return result;
//    }
//
//    private Collection<String> doBetweenSharding(final Collection<String> availableTargetNames,
//                                                 final ShardingValue shardingValue) {
//        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
//        Range<Long> range = shardingValue.getValueRange();
//        for (Long i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
//            for (String each : availableTargetNames) {
//                if (each.endsWith("_" + (i % availableTargetNames.size()))) {
//                    result.add(each);
//                }
//            }
//        }
//        return result;
//    }
//
//	@Override
//	public Collection<String> doSharding(Collection<String> availableTargetNames,
//			Collection<ShardingValue> shardingValues) {
//		ShardingValue shardingValue = shardingValues.iterator().next();
//        switch (shardingValue) {
//            case SINGLE:
//                return doEqualSharding(availableTargetNames, shardingValue);
//            case LIST:
//                return doInSharding(availableTargetNames,  shardingValue);
//            case RANGE:
//                return doBetweenSharding(availableTargetNames, shardingValue);
//            default:
//                throw new UnsupportedOperationException();
//        }
//	}
}
